import request from "@/router/axios";
import Ajax from "axios";

const DEFAULT_PASSWORD = "123456";

/**
 * 上传图片
 * @param {Blob} blob
 * @param {string} type
 */
export const UploadImage = (blob, type) => {
  let suffix = "jpg";
  let contentType = "image/jpeg";
  if (type === "image/png") {
    suffix = "png";
    contentType = "application/x-plt";
  }

  return new Promise((resolve, reject) => {
    request({
      url: "/base/sys/admin/getOSSUploadUrl",
      method: "POST",
      data: {},
      params: {
        suffix,
        contentType,
      },
    })
      .then((res) => {
        Ajax.put(res.data.data.bussData.uploadUrl, blob, {
          headers: {
            "Content-Type": contentType,
          },
        })
          .then(() => {
            resolve({
              data: res.data.data,
            });
          })
          .catch((err) => {
            reject({
              error: err,
            });
          });
      })
      .catch((error) => ({
        error,
      }));
  })
    .then((data) => ({
      data: data.data,
    }))
    .catch((error) => ({
      error,
    }));
};

/**@desc 轮播图上下架 */
export const PromisePicturePutAway = (id, post) => {
  return request({
    url: "/admin/slideShow/putAway",
    data: {
      id,
      isPutAway: post,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
  });
};

/**@desc 商品管理上下架 */
export const PromiseProductPutAway = (id, post) => {
  return request({
    url: "/admin/product/putAway",
    data: {
      id,
      isPutAway: post,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
  });
};

/**@desc 问题管理上下架 */
export const PromiseQuestionPutAway = (id, post) => {
  return request({
    url: "/admin/question/putAway",
    data: {
      id,
      isPutAway: post,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
  });
};

/**@desc 首页弹窗上下架 */
export const PromisePopupPutAway = (id, post, type) => {
  return request({
    url: "/admin/popup/putAway",
    data: {
      id,
      isPutAway: post,
      type,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
  });
};

/**@desc 重置员工密码 */
export const PromiseResetPassword = (id) => {
  return request({
    url: "/admin/staff/updatePassword",
    data: {
      id,
      password: DEFAULT_PASSWORD,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/json;",
    },
  });
};

/**@desc 提现审批 */
export const PromiseWithdrawAudit = (id) => {
  return request({
    url: "/admin/withdraw/audit",
    data: { id, auditStatus: "PASS" },
    method: "POST",
  });
};

// 商家上下架
export const PromiseShopPutAway = (id, post) => {
  return request({
    url: "/admin/merchant/putAway",
    data: {
      id,
      isPutAway: post,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
  });
};

/**@desc 展会管理上下架 */
export const PromiseExhibitionPutAway = (id, post) => {
  return request({
    url: "/admin/exhibition/putAway",
    data: {
      id,
      isPutAway: post,
    },
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
  });
};

// 获取城市字典树
export const SyncGetCityDic = () => {
  return request({
    url: "/base/sys/admin/loadDictionary",
    data: {
      codeType: "province",
    },
    method: "POST",
  })
    .then((res) => ({
      data: res.data,
    }))
    .catch((error) => ({
      error,
    }));
};

// 获取权限字典树
export const SyncGetRoleDic = () => {
  return request({
    url: "/admin/role/getAllAppPermission",
    data: {},
    method: "POST",
  })
    .then((res) => ({
      data: res.data,
    }))
    .catch((error) => ({
      error,
    }));
};

// 指派員工维修
export const PromiseDesignateStaff = (id, staffId) => {
  return request({
    url: "/admin/maintainOrder/designate",
    method: "POST",
    data: {
      id,
      staffId,
    },
  });
};

// 指派商家
export const PromiseDesignateMerchant = (id, merchantId) => {
  return request({
    url: "/admin/order/designate",
    method: "POST",
    data: {
      id,
      merchantId,
    },
  });
};

// 获取未接订单数
export const SyncRequestReceiving = () => {
  return request({
    url: "/admin/maintainOrder/notReceiving",
    method: "POST",
    data: {},
  })
    .then((res) => ({
      data: res.data,
    }))
    .catch((error) => ({
      error,
    }));
};

// 获取维修问题类型
export const SyncQuestionTypes = () => {
  return request({
    url: "/admin/question/adminPage",
    method: "POST",
    data: { pageIndex: 1, pageSize: 9999 },
    headers: {
      "Content-Type": "application/json;",
    },
  })
    .then((res) => ({
      data: res.data,
    }))
    .catch((error) => ({
      error,
    }));
};

// 获取订单设置
export const SyncRequestSetting = () => {
  return request({
    url: "/admin/orderSetting/query",
    method: "POST",
    data: {},
  })
    .then((res) => ({
      data: res.data,
    }))
    .catch((error) => ({
      error,
    }));
};

// 设置订单
export const PromiseSaveSetting = (hours, id) => {
  const data = {
    time: hours,
  };
  if (id) {
    data.id = id;
  }
  return request({
    url: "/admin/orderSetting/save",
    method: "POST",
    data,
  });
};

// 审批商户
export const PromiseAuditMerchant = (id, auditStatus) => {
  return request(
    {
      url: "/admin/merchant/audit",
      method: "POST",
      data: {
        id,
        auditStatus,
      },
    },
    60
  );
};
